<?php

namespace app\controller\index\v1;

use app\model\index\User;
use app\model\index\Users;
use lico\MVC\Controller;
use lico\Tool\Apidoc;
use lico\Tool\Cache;
use lico\Tool\Captcha;
use lico\Tool\Encrypt;
use lico\Tool\Excel;
use lico\Tool\Qrcode;
use lico\Tool\Session;
use lico\Tool\Thumb;
use lico\Tool\Upload;

class Index extends Controller
{

    /**
     * 入口
     */
    public function index()
    {

        // $result = "访问success";

        $result = (new User())->get();
        // return $result;
        // return $this->jsonView('获取成功', $result, 1);
        $session = new Session();
        $session->set('test','123');

        $this->assign('session', $session->get());
        $this->assign('result', $result);
        return $this->display();
    }

    /**
     * excel数据导出
     * @param string a
     * @param string b
     * @return array
     */
    public function excelOut()
    {
        echo 'excelOut';
        $Excel = new Excel();

        $filename = STATIC_PATH . 'file/志愿者.xls';
        $data = $Excel->export($filename, 2, true);
        p($data);
    }

    /**
     * excel导进表格
     * @param string a
     * @param string b
     * @return array
     */
    public function excelIn()
    {
        echo 'excelIn';
        $Excel = new Excel();

        $filename = '导入.xls';
        $head = ['编号' => 'id', '性别' => 'sex', '用户名' => 'name'];
        $data = (new Users())->get();
        $Excel->import($head, $data, $filename);
    }

    /**
     * api文档生成
     */
    public function apidoc()
    {
        $Apidoc = new Apidoc();
        $class = $Apidoc->buildApiDocument(__CLASS__);
        p($class);
    }

    /**
     * 二维码
     */
    public function qrcode()
    {
        // echo '二维码';
        $Qrcode = new Qrcode();
        $info = 'http://www.baidu.com?lico=' . rand(10000, 99999);
        $Qrcode->create($info);
        $Qrcode->view();
        // $Qrcode->save('./qrcode.png');
    }

    /**
     * 加密算法
     */
    public function encrypt()
    {
        $Encrypt = new Encrypt('openssl');
        $handler = $Encrypt->getHandler();

        $data = 'licosad12345678910LICO';
        // $result = $handler->sign($data);p($result);
        // $sign = 'JagOCZGCjBXIseXIm3izsS8IBvfhb7gGA4jeNcYEUaPCODd6XxjGnOVirl/ZsZ0qYb5//VkC9ny4Id0ZaHhyESvByQ/wuDwMb3qbY2QWVzkQJTvanIFEsCeBAbrk3BUcvZKQwac1T5D8MS7Fse6XMz3Qkm1i7vkSJFq3untx5gs=';
        // $result = $handler->verify($data, $sign);d($result);
        $result = $handler->encript($data);
        d($result);

        $encrypt = '0Z23P6eo2w0kT1thHp6IMX49f8stVebhBOim+/yuY2YrhGIrTiLQNZfYBeiwAHKBcBqKSRz2bD5XhPLoA2Atjx4X2BGRuTdoA381yZwsOSVjVlhB2G8D/NgV/RyNIIUHdA4M/2fPf2obAy0CFWrbbe6M8/3PvWkVrycyHsb7qO0=';
        $result = $handler->decript($encrypt);
        d($result);

        // $encrypt = '9ad5ff992eb3e1e8b067fca192ebd592';
        // $result = $handler->verify($data, $encrypt);d($result);
    }

    /**
     * 验证码
     */
    public function captcha()
    {

        //digit,character,arithmetic,chinese
        $Captcha = new Captcha('digit', 4);
        // $Captcha = new Captcha('character', 4);
        // $Captcha = new Captcha('chinese', 4);
        // $Captcha = new Captcha('arithmetic', 6);

        $Captcha->draw();
    }

    /**
     * 缩略图
     */
    public function thumb()
    {
        $config = [
            'sourcePath' => STATIC_PATH . 'image/test2.jpg', //原图片路径
            'thumbFolder' => STATIC_PATH,
            'filename' => 'image/test2_300', //缩略图名，这个格式将影响返回缩略图路径格式
            'thumbType' => 1,
        ];
        $Thumb = new Thumb($config);
        $thumbname = $Thumb->draw();
        p($thumbname);
        p($Thumb->getError());
    }

    /**
     * 上传图片
     */
    public function upload()
    {
        $config = [
            'saveFolder' => 'public/static/image', //原图片路径
        ];
        $Upload = new Upload('slice', $config);
        $uploadName = $Upload->save();
        p($uploadName);

        //切片上传时，需要获取批次id
        //切片唯一批次id
        $sliceUid = $Upload->getHandler()->getSliceUid();
        p($Upload->getHandler()->getError()); //错误信息
    }

    public function uploadtest()
    {

        return $this->display('upload');
    }

    /**
     * 缓存测试
     */
    public function cache()
    {
        $Cache = new Cache('redis');
        $handler = $Cache->getHandler();
        p($handler);
        $handler->publish('chat', '你好');

        // p($handler->get('test'));
        // $result = $Cache->queue();
    }

    /**
     * 缓存测试
     */
    public function redisQuere()
    {
        //不适合做聊天室，一个连接一条线程
        //可后台挂起一个长连接配合队列做消息队列推送
        try {

            $Cache = new Cache('redis');
            $handler = $Cache->getHandler();
            $handler->setOption(\Redis::OPT_READ_TIMEOUT, -1);
            $handler->subscribe(['chat'],function($handler, $chan, $msg){
                echo "channel:".$chan.",message:".$msg."\n";
            });
        } catch (\Exception $e) {
            echo $e->getMessage();
        }

    }

    /**
     * 缓存清除
     */
    public function cacheInit()
    {
        $Cache = new Cache('redis');
        $handler = $Cache->getHandler();
        $handler->delete('num', 'queue');

        echo '清除成功';
    }

    /**
     * 测试
     */
    public function test()
    {
        $a = [1 => 'a', 2 => 'b', 'c' => 'c', 'd' => 'd'];
        $b = [1 => 'u', 2 => 'e', 3 => 'v', 'c' => 'w', 'd' => 'x', 'y' => 'y', '60' => 'z'];

        p($a + $b);
        p(array_merge($a, $b));
    }
}
